package online.shenme.itcast.ui;

import online.shenme.itcast.entity.ExamInfo;
import online.shenme.itcast.entity.QuestionInfo;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 考试界面
 */
public class ExamFrame extends JFrame {
    private static final long serialVersionUID = -5355432125621015300L;
    private final Option[] options = new Option[4];

    public ExamFrame() {
        init();
    }

    private void init() {
        setTitle("传智考试系统");
        setSize(600, 380);
        setContentPane(createContentPane());
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                clientContext.submit();
            }
        });

    }

    private JPanel createContentPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(6, 6, 6, 6));
        ImageIcon icon = new ImageIcon(
                Objects.requireNonNull(getClass().getResource("logo.gif")));

        pane.add(BorderLayout.NORTH,
                new JLabel(icon));

        pane.add(BorderLayout.CENTER,
                createCenterPane());

        pane.add(BorderLayout.SOUTH,
                createToolsPane());

        return pane;
    }

    private JPanel createCenterPane() {
        JPanel pane = new JPanel(new BorderLayout());

        JLabel examInfo =
                new JLabel("姓名:XXX 考试:XXX 考试时间:XXX",
                        JLabel.CENTER);
        //注意
        info = examInfo;

        pane.add(BorderLayout.NORTH, examInfo);

        pane.add(BorderLayout.CENTER,
                createQuestionPane());
        pane.add(BorderLayout.SOUTH,
                createOptionsPane());
        return pane;
    }

    private JPanel createOptionsPane() {
        JPanel pane = new JPanel();
        Option a = new Option(0, "A");
        Option b = new Option(1, "B");
        Option c = new Option(2, "C");
        Option d = new Option(3, "D");
        options[0] = a;
        options[1] = b;
        options[2] = c;
        options[3] = d;
        pane.add(a);
        pane.add(b);
        pane.add(c);
        pane.add(d);
        return pane;
    }

    private JScrollPane createQuestionPane() {
        JScrollPane pane = new JScrollPane();
        pane.setBorder(new TitledBorder("题目"));
        JTextArea questionArea = new JTextArea();
        question = questionArea;
        questionArea.setText("问题\nA.\nB.");
        questionArea.setLineWrap(true);
        questionArea.setEditable(false);
        pane.getViewport().add(questionArea);
        return pane;
    }

    private JPanel createToolsPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(0, 10, 0, 10));
        questionCount =
                new JLabel("题目:20 的 1题");
        JLabel time =
                new JLabel("剩余时间:");
        timer = time;
        pane.add(BorderLayout.WEST, questionCount);
        pane.add(BorderLayout.EAST, time);
        pane.add(BorderLayout.CENTER, createBtnPane());
        return pane;
    }

    private JPanel createBtnPane() {
        JPanel pane = new JPanel(new FlowLayout());
        prev = new JButton("上一题");
        next = new JButton("下一题");
        JButton send = new JButton("交卷");

        pane.add(prev);
        pane.add(next);
        pane.add(send);

        prev.addActionListener(e -> clientContext.prev());
        next.addActionListener(e -> clientContext.next());
        send.addActionListener(e -> clientContext.submit());

        return pane;
    }


    static class Option extends JCheckBox {
        private static final long serialVersionUID = 5569918347028393191L;
        int val;

        public Option(int val, String txt) {
            super(txt);
            this.val = val;
        }
    }

    public void setClientContext(ClientContext clientContext) {
        this.clientContext = clientContext;
    }

    private ClientContext clientContext;
    private JLabel info;
    private JLabel questionCount;
    private JTextArea question;

    public void updateView(
            ExamInfo examInfo, QuestionInfo questionInfo) {
        info.setText(examInfo.toString());
        //"题目:20 的 1题"
        questionCount.setText("题目:" +
                examInfo.getQuestionCount() + " 的 " +
                (questionInfo.getQuestionIndex() + 1) + "题");
        question.setText(questionInfo.toString());
        updateOptions(questionInfo.getUserAnswers());
        updateButton(examInfo.getQuestionCount(),
                questionInfo.getQuestionIndex());
    }

    private void updateOptions(List<Integer> userAnswers) {
        for (Option o : options) {
            o.setSelected(false);
            if (userAnswers.contains(o.val)) {
                o.setSelected(true);
            }
        }
    }

    public List<Integer> getUserAnswers() {
        List<Integer> ans = new ArrayList<>();
        for (Option o : options) {
            if (o.isSelected()) {
                ans.add(o.val);
            }
        }
        return ans;
    }

    private JLabel timer;

    //更新时间到页面
    public void updateTime(long show) {
        long h = show / 1000 / 60 / 60;
        long m = show / 1000 / 60 % 60;
        long s = show / 1000 % 60;
        if (m <= 5) {
            timer.setForeground(Color.red);
        }
        timer.setText(h + ":" + m + ":" + s);
    }

    private JButton next;
    private JButton prev;

    private void updateButton(int count, int index) {
        prev.setEnabled(index != 0);
        next.setEnabled(index != count - 1);
    }
}












